<?php
/**  
* Page.php
* 
*/


require_once(CLASS_PATH."/__autoload.php");

class Page
{
   protected $css;
   protected $css_linked;
   public $css_linked_info;
   protected $css_linked_used;
   protected $css_is_local;
   protected $css_common;
   protected $css_page;
   protected $css_module;
   protected $css_id;

   protected $js;
   protected $js_linked;
   protected $js_linked_info;
   protected $js_linked_used;
   protected $js_is_local;
   protected $js_common;
   protected $js_page;
   protected $js_module;
   protected $js_is_top;

   protected $load_args;
   protected $load_data;
   protected $load_stat;
   protected $save_args;
   protected $save_data;
   protected $save_stat;
   protected $save_data_flag;

   protected $title;
   protected $viewport;
   protected $pragma;
   protected $expire;
   protected $favic;
   protected $desc;
   protected $keywd;
   protected $body;


   public function __construct()
   {
      $this->css 				= "";
      $this->css_linked 		= "";
      $this->css_linked_info	= array();
      $this->css_linked_used 	= array();
      $this->css_is_local 		= true;
      $this->css_common 		= "";
      $this->css_page 			= "";
      $this->css_module 		= "";
      $this->css_id 			= "";

      $this->js 				= "";
      $this->js_linked 			= "";
      $this->js_linked_info 	= array();
      $this->js_linked_used 	= array();
      $this->js_is_local 		= true;
      $this->js_common 			= "";
      $this->js_page 			= "";
      $this->js_module 			= "";
      $this->js_is_top 			= false; //true;   // false

      $this->load_args 			= array();
      $this->load_data 			= array();
      $this->load_stat 			= "";
      $this->save_args 			= array();
      $this->save_data 			= array();
      $this->sava_stat 			= "";
      $this->save_data_flag 	= false;

      $this->title 				= "";
	  $this->viewport 			= "";
	  $this->pragma 			= "";  
      $this->expire 			= "";
      $this->desc 				= "";
      $this->keywd 				= "";
	  $this->script				= "";
	  $this->favicon 			= "";
   }


//------------- 메서드(페이지생성)

   public function create()
   {
      $this->register_links();

      if ($this->save_data_flag)
         $this->save_data();

      $this->load_data();

      // 백엔드에서 로딩한 데이터에 기반해 페이지 초기화
      $this->set_title();
      $this->set_meta();
	  $this->set_favicon();
      $this->set_css_id();

      $this->set_js_common();
      $this->set_js_page();
      $this->set_css_common();
      $this->set_css_page();

      $header 	= $this->get_header();
      $content 	= $this->get_content();
      $footer 	= $this->get_footer();

      $this->body = <<<EOD
	  
<article id='site_main' class='content' >

$header
$content
$footer

</article>	<!-- site_main  -->

EOD;
      return $this->body;
   }


//------------- 메서드(페이지조립)

   public function get_page()
   {
      if (empty($this->css_id))
         $css_id 	= "";
      else
         $css_id 	= " id=\"".$this->css_id."\"";

      $doctype 		= $this->get_doctype();
      $title 		= $this->get_title();	  
      $meta 		= $this->get_meta();
	  $favicon 		= $this->get_favicon();
	  
      if ($this->js_is_top)
      {
         $js_top 	= $this->get_all_js();
         $js_btm 	= "";
      }
      else
      {
         $js_top 	= "";
         $js_btm 	= $this->get_all_js();
      }

      $css 			= $this->get_all_css();

      return <<<EOD
$doctype
<html lang="ko" class="no-js" >
<head>
$meta
$title
$favicon
$css
$js_top
</head>
<body{$css_id} class='nocopy' >
$this->body
$js_btm
</body>
</html>

EOD;
   }


//------------- 메서드(doctype, meta, title, favicon)

   public function get_doctype()
   {
      return <<<EOD
<!DOCTYPE html>

EOD;
   }

   public function get_meta()
   {
	   
	   
      $meta = <<<EOD
<meta charset="utf-8" />

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

<!--



<meta http-equiv="X-UA-Compatible" content="IE=7" />

<meta http-equiv="X-UA-Compatible" content="IE=8" />

<meta http-equiv="X-UA-Compatible" content="IE=Edge" />

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />

-->


EOD;
	

      if (!empty($this->viewport))
      {
         $meta .= <<<EOD
<meta name="viewport" content="{$this->viewport}" />

EOD;
      }
	  

      if (!empty($this->pragma))
      {
         $meta .= <<<EOD
<meta http-equiv="pragma" content="{$this->pragma}" />

EOD;
      }
	  
      if (!empty($this->expire))
      {
         $meta .= <<<EOD
<meta http-equiv="expire" content="{$this->expire}" />

EOD;
      }
	  
	  if (!empty($this->desc))
      {
         $meta .= <<<EOD
<meta name="description" content="{$this->desc}" />

EOD;
      }

      if (!empty($this->keywd))
      {
         $meta .= <<<EOD
<meta name="keywords" content="{$this->keywd}" />

EOD;
      }

      return $meta;
   }


   public function get_title()
   {
      return <<<EOD
<title>{$this->title}</title>

EOD;
   }

   private function get_favicon()
   {
      return <<<EOD
<meta />	  
<link rel="shortcut icon" type="image/png" href="$this->favicon" />

EOD;
   }
   
//------------- 메서드(css)

   public function add_to_css_linked($keys)
   {
      $this->css_linked .= $this->manage_css_linked($keys);	  
   }

   public function add_to_css($css)
   {
      $this->css .= $css;
   }

   public function get_all_css()
   {
      $this->css_module = $this->css_linked;
      $this->css_module .= $this->create_css($this->css);

      return <<<EOD
<!-- Common CSS -->
$this->css_common
<!-- Page CSS -->
$this->css_page
<!-- Module CSS -->
$this->css_module

<!--[if IE 8]>
	
	<link rel="stylesheet" href="/2013p79/common/css/ie8.css" /> 

<![endif]-->  



EOD;
   }


//------------- 메서드(javascript)

   public function add_to_js_linked($keys)
   {
      $this->js_linked .= $this->manage_js_linked($keys);
   }

   public function add_to_js($js)
   {
      $this->js .= $js;
   }

   public function get_all_js()
   {
      $this->js_module = $this->js_linked;
      $this->js_module .= $this->create_js($this->js);

      return <<<EOD
<!-- Common JS -->
$this->js_common

<!--[if lt IE 9]>
 <!--   <script src='http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js' title='css3 인식모듈' > </script>
    <script src="/2013p79/common/js/selectivizr-min1.02.js" title='CSS3 의사클래스와 IE6 ~ IE8 속성셀렉트 적용지원, jQuery 뒤에 위치시킬것' > </script>
 	<script src='http://html5shim.googlecode.com/svn/trunk/html5.js' title='html5 태그 인식모듈' > </script>  -->
	<!-- <script src="../js/ie_applyHtml5.js"> </script>   IE 전용 css도 이곳으로 모아 ie 판별을 1회만 적용 토록 할 것-->
<![endif]-->

<!--[if IE 8]>
	
	

<![endif]-->    
    
<!--[if IE 7]>
 <!--   <script src="/2013p79/common/js/css3-mediaqueries.js" title='IE 6, IE 7 에서 미디어쿼리 지원' ></script>   -->
    
<![endif]--> 
    
<!-- Page JS -->
$this->js_page

<!-- Module JS -->
$this->js_module
EOD;
   }

//------------- 추상메서드(하위클래스에서 상속 후 오버라이드) 

   public function get_css_common()
   {
   }

   public function get_css_linked()
   {
   }

   public function get_css()
   {
   }

   public function get_js_common()
   {
   }

   public function get_js_linked()
   {
   }

   public function get_js()
   {
   }

   public function load_data()
   {
   }

   public function save_data()
   {
   }

   public function get_header()
   {
   }

   public function get_footer()
   {
   }

   public function get_content()
   {
   }

   public function set_title()
   {
   }

   public function set_meta()
   {
   }
   
   public function set_favicon()
   {
   }
   
   public function set_css_id()
   {
   }

   public function register_links()
   {
   }
   
//------------- 메서드 세부구현(css지원) 

   private function manage_css_linked($keys)
   {
      $css = "";

      if (empty($keys))
         return "";

      if (!is_array($keys))
         $keys = array($keys);
             
       
      foreach ($keys as $k)
      {
      
         if (!array_key_exists($k, $this->css_linked_info))
         {
            error_log("Page::manage_css_linked: Key \"".$k."\" missing");
            continue;
         }
       
         // 중복추가 방지
         if (array_search($k, $this->css_linked_used) === false)
         {
            $this->css_linked_used[] = $k; 
            $css .= $this->create_css_linked($k);    
         }
      }
      
      return $css;
   }


   private function create_css_linked($k)
   {
      if ($this->css_is_local)
         $path = $this->css_linked_info[$k]["loc_path"];
      else
         $path = $this->css_linked_info[$k]["aka_path"];
         
      if (empty($this->css_linked_info[$k]["media"]))
         $media = "all";
      else
         $media = $this->css_linked_info[$k]["media"];

      return <<<EOD
<link href="$path" rel="stylesheet" media="$media" />

EOD;
   }

   private function create_css($css)
   {
      if (!empty($css))
      {
         return <<<EOD
<style media="all" > $css </style>

EOD;
      }
      else
      {
         return "";
      }
   }

   private function set_css_common()
   {
      $this->css_common = $this->manage_css_linked($this->get_css_common());
   }

   private function set_css_page()
   {
      $this->css_page = $this->manage_css_linked($this->get_css_linked());
      $this->css_page .= $this->create_css($this->get_css());
   }


//------------- 메서드 세부구현(javascript지원) 

   private function manage_js_linked($keys)
   {
      $js = "";

      if (empty($keys))
         return "";

      if (!is_array($keys))
         $keys = array($keys);

      foreach ($keys as $k)
      {
         if (!array_key_exists($k, $this->js_linked_info))
         {
            error_log("Page::manage_js_linked: Key \"".$k."\" missing");
            continue;
         }

         // 중복추가 방지
         if (array_search($k, $this->js_linked_used) === false)
         {
            $this->js_linked_used[] = $k;
            $js .= $this->create_js_linked($k);
         }
      }

      return $js;
   }

   private function create_js_linked($k)
   {
      if ($this->js_is_local)
      {
         $path = $this->js_linked_info[$k]["loc_path"];
      }   
      else
      {
         $path = $this->js_linked_info[$k]["aka_path"];
      }   

      return <<<EOD
<script src="$path" > </script>

EOD;
   }

   private function create_js($js)
   {
      if (!empty($js))
      {
         return <<<EOD
<script> $js </script>

EOD;
      }
      else
      {
         return "";
      }
   }

   private function set_js_common()
   {
      $this->js_common = $this->manage_js_linked($this->get_js_common());
   }

   private function set_js_page()
   {
      $this->js_page = $this->manage_js_linked($this->get_js_linked());
      $this->js_page .= $this->create_js($this->get_js());
   }
}
?>

